package com.meng.shiro;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * shiro Filter 拦截配置
 *
 * @author MENG
 * @version 2018/1/23
 * @see
 */
@Configuration
public class ShiroConfig
{

    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager sm)
    {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(sm);

        //拦截器.
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();

        // 配置不会被拦截的链接 顺序判断


        //<!-- 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/**", "anon");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    /**
     * shiro  验证
     * @return
     */
    @Bean
    public CustomShiroRealm getCustomShiroRealm()
    {
        CustomShiroRealm customShiroRealm = new CustomShiroRealm();

        return customShiroRealm;
    }


    /**
     * SecurityManager 安全管理器
     * @return
     */
    @Bean
    public SecurityManager securityManager()
    {
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();

        securityManager.setRealm(getCustomShiroRealm());

//        securityManager.setRememberMeManager(getRememberMeManager());

        return securityManager;
    }


//    /**
//     * cookie对象;
//     * rememberMeCookie()方法是设置Cookie的生成模版，比如cookie的name，cookie的有效时间等等。
//     * @return
//     */
//    @Bean
//    public SimpleCookie getRememberMeCookie()
//    {
//        //System.out.println("ShiroConfiguration.rememberMeCookie()");
//        //这个参数是cookie的名称，对应前端的checkbox的name = rememberMe
//        SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
//
//        System.out.println("11-------------------------------------------------");
//        //<!-- 记住我cookie生效时间2天 ,单位秒;-->
//        simpleCookie.setMaxAge(24*60*60*2);
//
//        return simpleCookie;
//    }
//
//    /**
//     * cookie管理对象;
//     * rememberMeManager()方法是生成rememberMe管理器，而且要将这个rememberMe管理器设置到securityManager中
//     * @return
//     */
//    @Bean
//    public CookieRememberMeManager getRememberMeManager()
//    {
//        //System.out.println("ShiroConfiguration.rememberMeManager()");
//        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
//
//        cookieRememberMeManager.setCookie(getRememberMeCookie());
//
//        //rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)
//        cookieRememberMeManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
//
//        return cookieRememberMeManager;
//    }

}
